<?php

namespace app\api\controller;

use app\api\service\Orders as OrdersService;
use app\common\controller\Api;
use think\exception\ValidateException;


/**
 * 订单接口
 */
class Orders extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    // protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = '*';
    private $ordersService;

    public function __construct()
    {
        parent::__construct();
        $this->ordersService = new OrdersService;
    }


    /**
     * 用户下单
     *
     */
    public function submit()
    {
        $data = $this->request->post();
        // 参数校验
        $validate  = validate('Orders');
        if (!$validate->scene('submit')->check($data)) {
            throw new ValidateException($validate->getError());
        }
        $data['userId'] = $this->auth->__get('id');
        $ret = $this->ordersService->submitOrder($data);
        return $this->success('返回成功', $ret);
    }


    /**
     * 订单支付
     *
     */
    public function payment()
    {
        $data = $this->request->put();
        // 参数校验
        $validate  = validate('Orders');
        if (!$validate->scene('payment')->check($data)) {
            throw new ValidateException($validate->getError());
        }
        $data['userId'] = $this->auth->__get('id');
        $ret = $this->ordersService->paymentOrder($data);
        return $this->success('返回成功', $ret);
    }

    /**
     * 历史订单
     *
     * @return void
     */
    public function historyOrders()
    {
        $data = $this->request->get();
        // 参数校验
        $validate  = validate('Orders');
        if (!$validate->scene('historyOrders')->check($data)) {
            throw new ValidateException($validate->getError());
        }
        $data['userId'] = $this->auth->__get('id');
        $list = $this->ordersService->showHistoryOrders($data);
        return $this->success('返回成功', $list);
    }


    /**
     * 订单详情
     *
     * @param int $id
     */
    public function orderDetail($id)
    {
        $data['id'] = $id;
        $data['userId'] = $this->auth->__get('id');
        $ret = $this->ordersService->showOrderDetail($data);
        return $this->success('返回成功', $ret);
    }

    /**
     * 取消订单
     *
     * @param int $id
     */
    public function cancel($id)
    {
        $data['id'] = $id;
        $data['userId'] = $this->auth->__get('id');
        $this->ordersService->cancelOrder($data);
        return $this->success();
    }

    /**
     * 再来一单
     *
     * @param int $id
     */
    public function repetition($id)
    {
        $data['id'] = $id;
        $data['userId'] = $this->auth->__get('id');
        $this->ordersService->repetitionOrder($data);
        return $this->success();
    }
    
     /**
     * 催单
     *
     */
    public function reminder($id)
    {
        $data['id'] = $id;
        $data['userId'] = $this->auth->__get('id');
        $this->ordersService->reminderOrder($data);
        return $this->success();
    }
}
